import wx
from pref import prefs
from pyo import pa_get_output_devices, pa_get_input_devices


class PrefFrame(wx.Frame):
    def __init__(self):#,returndest, driver
        # read file or import ?
        # self.ret = returndest
        self.driver, self.ind = pa_get_output_devices()
        self.InDriver, self.InInd = pa_get_input_devices()
        temp = pa_get_input_devices()
        print temp
        print self.driver, self.ind
        # self.drivDict = {ind:driver for ind, driver in pa_get_output_devices()}
        # print self.drivDict 
        wx.Frame.__init__(self,None, id = -1, title="Preferences",pos =[30,30], size = [300,200])
        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour("#dddddd")
        self.box = wx.FlexGridSizer(rows=1,cols=2) # wx.VERTICAL
        bord = 5

        # txt channels
        self.txtnum = wx.StaticText(self.panel, id = -1, label = "Channels", size = wx.DefaultSize)
        self.box.Add(self.txtnum,border = bord, flag = wx.ALL|wx.ALIGN_RIGHT)
        # input channels
        self.channels = wx.Choice(self.panel, id= -1, size = wx.DefaultSize, choices = ["1","2","3","4","5","6","7","8","28"])
        self.box.Add(self.channels,border = bord, flag = wx.ALL)
        self.channels.SetSelection(prefs["channels"]-1)

        # txt ip
        self.txtnum = wx.StaticText(self.panel, id = -1, label = "Ipad IP", size = wx.DefaultSize)
        self.box.Add(self.txtnum,border = bord, flag = wx.ALL|wx.ALIGN_RIGHT)
        # input ip
        self.textbox = wx.TextCtrl(self.panel, -1, size = (100, -1), style=wx.TE_PROCESS_ENTER)
        self.box.Add(self.textbox,border = bord, flag = wx.ALL)
        self.textbox.SetValue(prefs["IpadIP"])

        # txt driver
        self.txtnum = wx.StaticText(self.panel, id = -1, label = "Output Driver", size = wx.DefaultSize)
        self.box.Add(self.txtnum,border = bord, flag = wx.ALL|wx.ALIGN_RIGHT)
        # input driver
        self.outputdriver = wx.Choice(self.panel, id= -1, size = wx.DefaultSize, choices = self.driver)
        self.box.Add(self.outputdriver,border = bord, flag = wx.ALL)
        self.outputdriver.SetSelection(self.ind.index(prefs["audioDriver"]))

        # txt driver
        self.txtnum = wx.StaticText(self.panel, id = -1, label ="Input Driver", size = wx.DefaultSize)
        self.box.Add(self.txtnum,border = bord, flag = wx.ALL|wx.ALIGN_RIGHT)
        # input driver
        self.inputdriver = wx.Choice(self.panel, id= -1, size = wx.DefaultSize, choices = self.InDriver)
        self.box.Add(self.inputdriver,border = bord, flag = wx.ALL)
        self.inputdriver.SetSelection(self.InInd.index(prefs["audioDriverInput"]))


        # input ok
        self.ok = wx.Button(self.panel, id = -1, label = "ok", size = wx.DefaultSize)
        self.ok.Bind(wx.EVT_BUTTON, self.done)
        self.box.Add(self.ok,border = bord, flag = wx.ALL)

        self.panel.SetSizer(self.box)

    def done(self, evt):
        prefs["IpadIP"] = self.textbox.GetValue().encode('ascii','ignore')
        prefs["channels"] = self.channels.GetCurrentSelection()+1
        prefs["audioDriver"] = self.ind[int(self.outputdriver.GetCurrentSelection())]
        prefs["audioDriverInput"] = self.InInd[int(self.inputdriver.GetCurrentSelection())]

        f = open("pref.py", "w")
        f.write("prefs = %s" % str(prefs))
        f.close()
        self.Destroy()
        

# prefs = {
#   "channels":2,
#   "IpadIP":"192.168.2.17",
#   "audioDriver": None,
#   "recDir":None,
#   "saveDir":None,
#   "CVout":None
# }
